export function updatePermision(instance, val: string) {
  const _this = instance;
  let routers = [];
  let widgets = [];

  return new Promise((resolve, reject) => {
    _this.$axios.$get("/new/systemctl/getpermisions").then(res => {
      const permisions = res.data;

      if (res && permisions) {
        if (permisions.routers && permisions.routers.length > 0) {
          routers = permisions.routers;
        }
        if (permisions.widgets && permisions.widgets.length > 0) {
          widgets = permisions.widgets;
        }
        console.log(routers);
        console.log(widgets);

        let localPermision = JSON.stringify({
          routers: routers,
          widgets: widgets
        });
        instance.$store.commit("PermisionsStorage/setHasPermision", {
          routers: routers,
          widgets: widgets
        });
      }
    });
  });
}

export function getPermision(instance) {
  let routers = [];
  let widgets = [];
  let permisions = instance.store.state.PermisionsStorage.HasPermision;

  return permisions;
}

export function checkPermision(instance, permisionKey: string, type: string) {
  let permisions = instance.store.state.PermisionsStorage.HasPermision;

  if (type == "widgets") {
    if (
      permisions.widgets &&
      permisions.widgets.length > 0 &&
      permisions.widgets.indexOf(permisionKey) >= 0
    ) {
      return true;
    } else {
      return false;
    }
  } else if (type == "routers") {
    if (
      permisions.routers &&
      permisions.routers.length > 0 &&
      permisions.routers.indexOf(permisionKey) >= 0
    ) {
      return true;
    } else {
      return false;
    }
  } else {
    if (
      (permisions.routers &&
        permisions.routers.length > 0 &&
        permisions.routers.indexOf(permisionKey) >= 0) ||
      (permisions.widgets &&
        permisions.widgets.length > 0 &&
        permisions.widgets.indexOf(permisionKey) >= 0)
    ) {
      return true;
    } else {
      return false;
    }
  }
}
